package com.dgut.hisystem.controller;

import com.dgut.hisystem.entity.Advice;
import com.dgut.hisystem.entity.Drug;
import com.dgut.hisystem.entity.VO.DrugVO;
import com.dgut.hisystem.entity.VO.PrescriptionVO;
import com.dgut.hisystem.entity.VO.SimpleDrug;
import com.dgut.hisystem.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RestController
@RequestMapping("/Drug")

public class DrugController {
    @Autowired
    private DrugService drugService;
    @RequestMapping("/getAll")
    public List<Drug> getAll(HttpServletRequest request){
        /*if((request.getParameter("id") == null|| request.getParameter("id").equals(""))&&(request.getParameter("name") == null|| request.getParameter("name").equals(""))){
            return drugService.getAll();
        }
        else if((request.getParameter("id") != null)&&(request.getParameter("name") == null|| request.getParameter("name").equals(""))){
            long id = Long.parseLong(request.getParameter("id"));
            List<Drug> drug = null;
            drug.add(drugService.getDrugById(id));
            return drug;
        } else if((request.getParameter("id") == null|| request.getParameter("id").equals(""))&&(request.getParameter("name") != null)){
            long id = Long.parseLong(request.getParameter("id"));
            List<Drug> drug = null;
            drug.add(drugService.getDrugById(id));*/

        return drugService.getAll();
    }

    @RequestMapping("/getExpDrug")
    public List<Drug> getExpDrug(){
        return drugService.getExpDrug();
    }

    @RequestMapping("/getDrugById")
    public Drug getDrugById(Integer id){
        return drugService.getDrugById(id);
    }

    @RequestMapping("/getDrugByName")
    public Drug getDrugByName(String name){
        return drugService.getDrugByName(name);
    }

    @RequestMapping("/delete")
    public void delete(Integer id){
        drugService.delete(id);
    }

    @RequestMapping("/updatePrice")
    public void updatePrice(Integer id, Double price){
        drugService.updatePrice(id,price);
    }

    @RequestMapping("/updateAmount")
    public void updateAmount(Integer id, Integer amount){
        drugService.updateAmount(id,amount);
    }

    @RequestMapping("/selectAllPrescription")
    public List<PrescriptionVO> selectAllPrescription(){
        return drugService.selectAllPrescription();
    }

    @RequestMapping("/selectAdviceById")
    public Advice selectAdviceById(Integer id){
        return drugService.selectAdviceById(id);
    }

    @RequestMapping("/insertDrug")
    public void insertDrug(@RequestBody DrugVO drugVO){
        Drug d = new Drug();
        d.setYp_id(Long.parseLong(drugVO.getYp_id()));
        d.setYp_name(drugVO.getYp_name());
        d.setYp_standard(drugVO.getInput_9());
        d.setYp_form(drugVO.getYp_form());
        d.setYp_producer(drugVO.getYp_producer());
        d.setYp_company(drugVO.getYp_company());
        d.setYp_uprice(Double.parseDouble(drugVO.getYp_price()));
        d.setYp_mcompany(drugVO.getYp_mcompany());
        d.setYp_coef(drugVO.getYp_coef());
        d.setYp_pycode(drugVO.getYp_pycode());
        d.setYp_wbcode(drugVO.getYp_wbcode());
        d.setYp_anumber(drugVO.getYp_anumber());
        d.setYp_insurance(Integer.parseInt(drugVO.getYp_insurance()));
        d.setYp_category(drugVO.getYp_category());
        d.setYp_ratio(drugVO.getYp_ratio());
        d.setYp_datetime(null);
        d.setYp_amount(Integer.parseInt(drugVO.getYp_count()));
        d.setYp_rprice(0);
        drugService.insertDrug(d);
    }

    @RequestMapping("/outStorage")
    public List<SimpleDrug> outStorage(long cf_id) {
        try {
            return drugService.drugOutStorage(cf_id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
